<?php
/********************************************************
 * 
 * Copyright (c) 2007 - 2008
 * 
 * 要查看完整的版权信息和许可信息,请查看源代码中附带的 COPYRIGHT 文件,
 * 
 * 此文件的作用:用户控制器
 * 
 * @copyright Copyright (c) 2007 - 2008
 * @author 刘昌功 lcg635@126.com
 * 
 ********************************************************/

defined('APP_DIR') or die('404 Not Found');

FLEA::loadClass('Common_Action');

class Controller_User extends Common_Action {
	function __construct($ctlName)   {
		parent::__construct($ctlName);
		$this->tplDir = TPL_F;
		$data = array(
			'header' => TPL . $this->tplDir . 'header.php',
			'footer' => TPL . $this->tplDir . 'footer.php'
		);
		$this->tpl->assign($data);
		$rbac = FLEA::getSingleton('FLEA_RBAC');
		$this->user = $rbac->getUser();
	}

	function index() {}

	function login() {
		$_modUser = FLEA::getSingleton('Model_User');		
		if ( $this->_isPost() ) {
			$result = $_modUser->login($_POST);
			if ( !empty($result) )
				js_alert($result, null, url('user', 'login'));
			else {
				//$url = empty($_COOKIE['backurl']) ? url() : $_COOKIE['backurl'];
				redirect(url());
			}
		} else {
			$viewData['form'] = $_modUser->getForm('login');
			$this->_out('user/login', $viewData);
		}
	}

	function adminLogin() {
		$this->tplDir = TPL_A;
		$_modAdmin = FLEA::getSingleton('Model_Admin');
		if ( $this->_isPOST() ) {
			$this->tpl->enableCache = false;
			$result = $_modAdmin->login($_POST);
			if ( null != $result )
				$this->_throwRemind('登录失败', $result, null, $this->_url('adminLogin'));
			$backUrl = $_SESSION['backUrl'];
			unset($_SESSION['backUrl']);
			if ( null == $backUrl )
				redirect(url('admin'));
			else {
				redirect($backUrl);
			}
		} else {
			$viewData = array(
				'form' => $_modAdmin->getForm('login')
			);
			$this->_out('login', $viewData, 'admin');
		}
	}

	function logout() {
		$rbac = FLEA::getSingleton('FLEA_RBAC');
		$rbac->clearUser();
		redirect(url());
	}

function register() {
		$_modUser = FLEA::getSingleton('Model_User');
		if ( $this->_isPOST() ) {
			$result = $_modUser->register($_POST);
			if ( count($result) == 0 )
				js_alert('注册成功', null, url());
			else
				js_alert($result[0], null, url());
		} else {
			$viewData = array(
				'form' => $_modUser->getForm('register'),
				'majors' => FLEA::loadFile('Configs_Majors')
			);
			$this->_out('user/register', $viewData);
		}
	}
	
	function modify() {
		$this->checkLogined();
		$_modUser = FLEA::getSingleton('Model_User');
		if ( $this->_isPOST() ) {
			$result = $_modUser->update($_POST);
			if (!empty($result)) {
				foreach ($result as $re) {
					$err .= $re . ',';
				}
				js_alert($err, 'history.back();');
			}
			js_alert('修改成功！', null, $this->_url('modify'));
		} else {
			$userData = $_modUser->getInfo($this->user['userid']);
			$viewData = array(
				'form' => $_modUser->getForm('modify', $userData),
				'user' => $userData,
				'majors' => FLEA::loadFile('Configs_Majors')
			);
			$this->_out('user/modify', $viewData);
		}
	}
	
	function checkUser() {
		$_modUser = FLEA::getSingleton('Model_User');
		echo $_modUser->checkUser($_GET['username']);
	}
	
	function currentUserData() {
		if ( $this->_isAjax() ) {
			$this->tplDir = TPL_D;
			$this->_out('userData', $this->user);
		}
	}
}
